import App from '@/App.vue'
import * as directives from '@/directives'
import * as filters from '@/filters'
import '@/icons/components'
import i18n from '@/lang'
import '@/permission'
import '@/pwa/register-service-worker'
import router from '@/router'
import store from '@/store'
import { AppModule } from '@/store/modules/app'
import '@/styles/element-variables.scss'
import '@/styles/index.scss'
import '@/utils/error-log'
import axios from 'axios'
import ElementUI from 'element-ui'
import 'normalize.css'
import Vue, { DirectiveOptions } from 'vue'
import SvgIcon from 'vue-svgicon'
import { ChannelConfig } from './config/ChannelConfig'
import { Config } from './config/Config'
import { GameConfig } from './config/GameConfig'
import { PrivacyConfig } from './config/PrivacyConfig'
import { HttpClient } from './httpclient/HttpClient'
import { DailyDataSummaryModel } from './store/model/DailyDataSummaryModel'
import { DemoExcel } from './store/model/DemoExcel'
import { GameModel } from './store/model/GameModel'
import { OpenServerData } from './store/model/OpenServerData'
import { RoleModel } from './store/model/RoleModel'
import { UserModel } from './store/model/UserModel'
import { ZoneModel } from './store/model/ZoneModel'
import { Logger } from './utils/Logger'
import { TimeUtil } from './utils/TimeUtil'




Vue.use(ElementUI, {
  size: AppModule.size, // Set element-ui default size
  i18n: (key: string, value: string) => i18n.t(key, value)
})

Vue.use(SvgIcon, {
  tagName: 'svg-icon',
  defaultWidth: '1em',
  defaultHeight: '1em'
})

// Register global directives
Object.keys(directives).forEach(key => {
  Vue.directive(key, (directives as { [key: string]: DirectiveOptions })[key])
})

// Register global filter functions
Object.keys(filters).forEach(key => {
  Vue.filter(key, (filters as { [key: string]: Function })[key])
})


function loadingConfig(callback: Function) {
  let count: number = 0;
  Config.jsonConfig.forEach((value: string, key: string) => {
    axios.get(value).then(res => {
      if (key == PrivacyConfig.Config_Name) {
        PrivacyConfig.json2Config(res.data)
      }
      else if (key == ChannelConfig.Config_Name) {
        ChannelConfig.json2Config(res.data);
      }
      else if (key == GameConfig.Config_Name) {
        GameConfig.json2Config(res.data)
      }
      count++;
      if (count >= Config.jsonConfig.size) {
        callback && callback()
      }
    })
  });
}

function getQueryString(name: string) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]); return null;
}

function gminit() {
  Vue.config.productionTip = false
  GameModel.instance.gameId = parseInt(getQueryString("gameid"))
  if (isNaN(GameModel.instance.gameId)) {
    GameModel.instance.gameId = GameConfig.GameId.Legend;
  }
  Logger.log("gminit==", GameModel.instance.gameId);
  loadingConfig(() => {
    //Test---------
    DemoExcel.initTest();
    ZoneModel.initTest();
    DailyDataSummaryModel.initTest();
    OpenServerData.initTest();
    RoleModel.initTest();
    UserModel.initTest()
    //Test End-------

    TimeUtil.init()
    UserModel.instance.init();
    new Vue({
      router,
      store,
      i18n,
      render: (h) => h(App)
    }).$mount('#app')
  });
}

gminit();
Logger.log("NODE_ENV======", process.env.NODE_ENV);
